import 'dart:convert';
import 'dart:math';

import 'package:crypto/crypto.dart';
import 'package:date_format/date_format.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:toast/toast.dart';
import 'package:url_launcher/url_launcher.dart';

class Screw {
  static final _dio = Dio();

  static get dioInstance => _dio;

  static String genMD5(String input) {
    var data = utf8.encode(input);
    return md5.convert(data).toString();
  }

  // https://pub.dev/packages/date_format
  static String genDateString() {
    return formatDate(DateTime.now(), [yyyy, '-', mm, '-', dd]);
  }

  static String getDateString(DateTime dateTime) {
    return formatDate(dateTime, [yyyy, '-', mm, '-', dd]);
  }

  static Color getRandomColor() {
    var rv = Random().nextInt(255);
    var gv = Random().nextInt(255);
    var bv = Random().nextInt(255);
    return Color.fromARGB(255, rv, gv, bv);
  }

  static void showToast(BuildContext context, String message) {
    Toast.show(message, context,
        duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
  }

  static void popup(BuildContext context, String message) {
    Toast.show(message, context,
        duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
  }

  static List<String> parseList(List data) {
    return data.cast<String>();
  }

  static Future<Map<String, dynamic>> getData(String url) async {
    var response = await _dio.get(url);
    return response.data;
  }

  static Future<List> getList(String url, String entry, String key) async {
    var items = [];
    var response = await _dio.get(url);
    var item = response.data;
    if (item['code'] == 200 && item['entry'] == entry) {
      items = item[key];
    }
    return items;
  }

  static Future<String> postData(String url, Map data) async {
    var response = await _dio.post(url, data: data);
    return response.data.toString();
  }

  static Future<String> getUserLocation() async {
    var docDir = await getApplicationDocumentsDirectory();
    return docDir.path;
  }

  static Future<String> getTempLocation() async {
    var tmpDir = await getTemporaryDirectory();
    return tmpDir.path;
  }

  static Future<String> getUserFilePath(String fileName) async {
    var docDir = await getApplicationDocumentsDirectory();
    return path.join(docDir.path, fileName);
  }

  static Future<String> getTempFilePath(String fileName) async {
    var tmpDir = await getTemporaryDirectory();
    return path.join(tmpDir.path, fileName);
  }

  static String joinPath(String dirPath, String fileName) {
    return path.join(dirPath, fileName);
  }

  static Future<void> openUrl(String url) async {
    if (await canLaunch(url)) await launch(url);
  }

  static Future<void> baiduSearch(String kw) async {
    var url = 'https://www.baidu.com/s?wd=' + kw;
    await openUrl(url);
  }
}
